package org.esgi.repository.impl;

import java.io.File;
import java.io.FilenameFilter;

import java.util.ArrayList;
import java.util.List;

import org.esgi.model.interfaces.IHistory;

import org.esgi.repository.interfaces.IHistoryRepository;

import org.esgi.transformer.impl.XMLHistoryAnswerTransformer;
import org.esgi.transformer.impl.XMLHistoryTransformer;
import org.esgi.transformer.impl.XMLQuestionTransformer;
import org.esgi.transformer.interfaces.IXMLHistoryTransformer;

import org.w3c.dom.Document;

public class XMLHistoryRepository extends XMLRepository implements IHistoryRepository 
{
	private File directory_;
	private IXMLHistoryTransformer historyTransformer_;
	
	public static final String XMLHistoryDefaultId = "00000000001";
	
	public XMLHistoryRepository(File dir)
	{
		super();
		this.setDirectory(dir);

		String namespace = "bmb";
		XMLHistoryAnswerTransformer historyAnswerTransformer = new XMLHistoryAnswerTransformer();
		historyAnswerTransformer.setDefaultNamespace(namespace);
		XMLQuestionTransformer questionTransformer = new XMLQuestionTransformer(historyAnswerTransformer);
		questionTransformer.setDefaultNamespace(namespace);
		XMLHistoryTransformer historyTransformer = new XMLHistoryTransformer(questionTransformer);
		historyTransformer.setDefaultNamespace(namespace);
		historyTransformer_ = historyTransformer;
	}
	
	public void setDirectory(File dir) { this.directory_ = dir; }
	
	public File getDirectory() { return this.directory_; }
	
	@Override
	public Boolean saveHistory(IHistory anHistory) 
	{
		if(anHistory instanceof IHistory)
		{
			if(anHistory.getId() == null)
				anHistory.setId(XMLHistoryDefaultId);
			File file = this.getFileHistoryWithId(anHistory.getId());
			Document root = this.historyTransformer_.historyToDocument(anHistory, this.getImpl());
			if(root != null)
			{
				root.getDocumentElement().setAttribute("xmlns:bmb", "http://bmb.org");
				return this.serializeDocument(root, file);
			}
		}
		return false;
	}

	@Override
	public Boolean removeHistory(IHistory anHistory) 
	{
		if(anHistory instanceof IHistory)
		{	
			File file = this.getFileHistoryWithId(anHistory.getId());			
			if(file != null)
				return file.delete();
		}
		return false;
	}

	@Override
	public IHistory getHistoryWithId(String anId) 
	{
		File file = this.getFileHistoryWithId(anId);
		if(file != null)
		{
			return this.loadHistoryFromFile(file);
		}
		return null;
	}
	
	@Override
	public File getFileHistoryWithId(String anId)
	{
		File dir = this.getDirectory();
		if(dir != null)
		{
			return new File(dir, anId+".xml");
		}
		return null;
	}
	
	@Override
	public IHistory getLastHistory()
	{
		return this.getHistoryWithId(XMLHistoryDefaultId);
	}
	
	@Override
	public File getLastFileHistory() 
	{
		return this.getFileHistoryWithId(XMLHistoryDefaultId);
	}

	@Override
	public List<IHistory> getAllHistories() 
	{
		File dir = this.getDirectory();
		if(dir != null)
		{
			File[] files = dir.listFiles(new FilenameFilter() {
				@Override
				public boolean accept(File arg0, String arg1) {
					return arg1.endsWith(".xml");
				}
			});
			List<IHistory> histories = new ArrayList<IHistory>();
			for(int i=0; i<files.length; i++)
			{
				IHistory history = this.loadHistoryFromFile(files[i]);
				if(history != null)
					histories.add(history);
			}
			return histories;
		}
		return null;
	}
	
	private IHistory loadHistoryFromFile(File aFile)
	{
		if(aFile != null && aFile.exists())
		{
			Document root = this.parseFile(aFile);
			if(root != null)
			{
				return this.historyTransformer_.historyFromElement(root.getDocumentElement());
			}
		}	
		return null;
	}
}
